//
//  SGJNetworkTools.m
//  Sewage
//
//  Created by 宗庆猛 on 2017/5/25.
//  Copyright © 2017年 宗庆猛. All rights reserved.
//

#import "SGJNetworkTools.h"
#import "AESCipher.h"
@implementation SGJNetworkTools

NSString* IP(NSString* suffix){
    
    return [NSString stringWithFormat:@"%@%@",@"http://192.168.0.212:8080",suffix];
}

+ (instancetype)sharedNetworkTool {
    static SGJNetworkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 设置相对路径
//        NSURL *BaseURL = [NSURL URLWithString:@"http://192.168.0.212:8080/sewage_api"];
//        //IP(/sewage_api)
//        // 实例化Manager
        instance = [[self alloc] init];
        //instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    });
    
    return instance;
}

- (void)requestWithURLString: (NSString *)URLString
                  parameters: (NSDictionary *)parameters
                      method: (NSString *)method
                    callBack: (void (^)(id))callBack {
    NSLog(@"请求参数%@",parameters);
    
    // 需要加密的参数
    NSMutableDictionary *encryptparams = [[NSMutableDictionary alloc]init];
    // 生成的新的参数
    NSMutableDictionary *newparams = [[NSMutableDictionary alloc]init];
    if ([URLString containsString:@"/api/"]) {
        // 遍历参数进行加密
        [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
            NSLog(@"value for key %@ is %@ ", key, value);
            if ([@"userId" isEqualToString:key]) {
                [newparams setValue:value forKey:key];
            }else if ([@"file_" isEqualToString:key]){
                [newparams setValue:value forKey:key];
            }else if ([@"totalFile" isEqualToString:key]){
                [newparams setValue:value forKey:key];
            }else{
                [encryptparams setValue:value forKey:key];
            }
        }];
        // 此处盐值以后需要修改存储
        NSString *salt = [[NSUserDefaults standardUserDefaults] objectForKey:@"salt"];
        // content是一个JSON串
        NSError *parseError = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:encryptparams options:NSJSONWritingPrettyPrinted error:&parseError];
        NSString *content = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        // AES加密content
        NSString *enCodeContent = [[NSString alloc]initWithString:aesEncryptString(content,salt)];
        // 将加密的数据添加到新的字典中
        [newparams setValue:enCodeContent forKey:@"content"];
    }else{
        [parameters enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
            [newparams setValue:value forKey:key];
        }];
    }
    
    if ([method isEqualToString:@"GET"]) {
        [self GET:URLString parameters:newparams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"请求响应%@",responseObject);
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
    
    if ([method isEqualToString:@"POST"]) {
        [self POST:URLString parameters:newparams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
            NSLog(@"请求响应%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
}

#pragma mark - 登录注册模块
-(void)getVerifyCodeWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        
        callBack(responseObject);
    }];
}

-(void)getSmsCodeWhenForgetPWDWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        NSLog(@"%@",responseObject);
        callBack(responseObject);
    }];

}

-(void)registerWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        
        callBack(responseObject);
    }];
}

-(void)resetPwdWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)loginWithUrlString: (NSString *)URLString parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack{


    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        NSLog(@"%@",responseObject);
        callBack(responseObject);
    }];
}

#pragma mark - 首页模块
-(void)loadHomeBannerWithUrlString: (NSString *)URLString parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)loadHomeGrapOrderWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)grabOrderWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)loadTaskDetailByIdWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

#pragma mark - 任务模块
-(void)loadCurrentTaskWithUrlString: (NSString *)URLString parameters: (NSDictionary *)parameters method: (NSString *)method callBack: (void(^)(id responseObject))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)quitTaskWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)examineTaskWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

-(void)routingInspectionWithUrlString:(NSString *)URLString parameters:(NSDictionary *)parameters method:(NSString *)method callBack:(void (^)(id))callBack{
    [self requestWithURLString:URLString parameters:parameters method:method callBack:^(id responseObject) {
        callBack(responseObject);
    }];
}

@end
